package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper,BaseCategoryTrademark> implements BaseCategoryTrademarkService {
    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;
    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {
        //  select * from base_category_trademark where category3_id = category3Id;
        //  先根据三级分类Id 来获取 品牌Id
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id",category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(baseCategoryTrademarkQueryWrapper);

        //CollectionUtils.isNotEmpty() 包含null,size=0等多种情况   而== null 只能用来判断是否为null
        //LIST.ISEMPTY()与COLLECTIONUTILS.ISEMPTY(LIST)的区别
        // list为null>>>list.isEmpty()函数出现NullPointerException(空指针异常)
        if(!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
            /*List<Long> idList = new ArrayList<>();
            //  获取到品牌Id 集合！
            for (BaseCategoryTrademark baseCategoryTrademark : baseCategoryTrademarkList) {
                Long trademarkId = baseCategoryTrademark.getTrademarkId();
                idList.add(trademarkId);
            }*/
            //  获取到品牌Id 集合！
            //  只有函数式接口才能使用拉姆达表达式！ 复制小括号，写死右箭头，落地大括号！
            List<Long> idList = baseCategoryTrademarkList.stream().map((baseCategoryTrademark) -> {
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());

            //  select * from base_trademark where id in (1,3)
            //  获取到品牌数据集合
            return baseTrademarkMapper.selectBatchIds(idList);
        }

        return null;
    }

    @Override
    public void remove(Long category3Id, Long trademarkId) {
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id",category3Id);
        baseCategoryTrademarkQueryWrapper.eq("trademark_id",trademarkId);
        baseCategoryTrademarkMapper.delete(baseCategoryTrademarkQueryWrapper);
    }

    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {
        //分类品牌管理 根据category3Id获取可选品牌列表

        //  select * from base_category_trademark where category3_id = category3Id;
        //  先根据三级分类Id 来获取 品牌Id
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id",category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(baseCategoryTrademarkQueryWrapper);

        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
            //  1,3
            List<Long> idList = baseCategoryTrademarkList.stream().map((baseCategoryTrademark) -> {
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());

            //  baseTrademarkMapper.selectList(null) 1,2,3
            List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectList(null);

            //  只留2！
            List<BaseTrademark> trademarkList = baseTrademarkList.stream().filter((baseTrademark) -> {
                return !idList.contains(baseTrademark.getId());
            }).collect(Collectors.toList());
            return trademarkList;
        }

        //  当这个分类Id 下没有品牌的时候， 显示所有品牌数据！
        return baseTrademarkMapper.selectList(null);
    }

    @Override
    public void save(CategoryTrademarkVo categoryTrademarkVo) {
        //  本质插入数据 base_category_trademark
        //  获取到对应的tmId 1,3
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
        if (!CollectionUtils.isEmpty(trademarkIdList)){
            //方法二 1
            ArrayList<BaseCategoryTrademark> baseCategoryTrademarkList = new ArrayList<>();

/*            for (Long tmId : trademarkIdList) {
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setTrademarkId(tmId);
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
//                方法一：在for循环中，可以这样插入，但是不推荐，为什么呢？
//                baseCategoryTrademarkMapper.insert(baseCategoryTrademark);
//                方法二：2  创建个集合 通过saveBatch方法批量插入，前提是Service接口 继承 IService接口
                baseCategoryTrademarkList.add(baseCategoryTrademark);*/

                //使用lambda表达式 代替for循环
            List<BaseCategoryTrademark> collect = trademarkIdList.stream().map((tmId) -> {
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setTrademarkId(tmId);
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                return baseCategoryTrademark;
            }).collect(Collectors.toList());

            this.saveBatch(collect);

        //}

//            this.saveBatch(baseCategoryTrademarkList);
        }
    }
}
